package com.dongline.test.join;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JoinReducer extends Reducer<Text,OrderWritable,OrderWritable, NullWritable> {

    @Override
    protected void reduce(Text key, Iterable<OrderWritable> values, Context context) throws IOException, InterruptedException {

        ArrayList<OrderWritable> orderWritables=new ArrayList<OrderWritable>();

        String name="";

        for(OrderWritable orderWritable:values)
        {
            if(orderWritable.isFlage())
            {

//                System.out.println("333");
                orderWritables.add(new OrderWritable(orderWritable.getId(),orderWritable.getPid(),orderWritable.getName(),orderWritable.getUser(),false));

            }
            else
            {

                name=orderWritable.getName();
            }
        }

        System.out.println(orderWritables.size());
        for (OrderWritable orderWritable:orderWritables)
        {

            context.write(new OrderWritable(orderWritable.getId(),orderWritable.getPid(),name,orderWritable.getUser(),false),NullWritable.get());
        }
    }
}
